﻿using System;
using System.Collections.Generic;
using System.Text;
using BCCommon;
using BCData.Common.WebsiteConfiguration;
using BCData.WeChatComponent.WeChatKeyword;
using BCDto.WeChatComponent.WeChatKeyword;
using BCEntity.WeChatComponent.WeChatKeyword;
using BCService.Utils;
using Microsoft.Extensions.Configuration;

namespace BCService.WeChatComponent.WeChatKeywordService
{
    /// <summary>
    /// 微信关键字服务
    /// </summary>
    public class WeChatKeywordService : IWeChatKeywordService
    {
        private readonly IConfiguration configuration;
        private readonly IWeChatKeywordData weChatKeywordData;
        private readonly IWebsiteConfigurationData websiteConfigurationData;

        public WeChatKeywordService(IConfiguration configuration,
            IWeChatKeywordData weChatKeywordData,
            IWebsiteConfigurationData websiteConfigurationData)
        {
            this.configuration = configuration;
            this.websiteConfigurationData = websiteConfigurationData;
            this.weChatKeywordData = weChatKeywordData;
        }
        /// <summary>
        /// 创建微信关键字信息
        /// </summary>
        /// <param name="dto">新的WeChatKeyword信息</param>
        /// <returns></returns>
        public WeChatKeywordDto Create(WeChatKeywordRequestDto dto)
        {
            if (this.weChatKeywordData.CheckKeywordExists(dto.Keyword))
            {
                throw new ArgumentException("当前关键字已存在。");
            }
            WeChatKeywordEntity entity = dto.As<WeChatKeywordEntity>();
            entity = weChatKeywordData.Create(entity);
            string websiteHost = configuration["CurrentlyImageHost"];
            entity.LocalUrl = string.Concat(websiteHost, entity.LocalUrl);
            return entity.As<WeChatKeywordDto>();
        }
        /// <summary>
        /// 删除一条微信关键字信息
        /// </summary>
        /// <param name="WeChatKeywordId">指定的WeChatKeyword信息Id</param>
        /// <returns></returns>
        public bool Delete(int WeChatKeywordId)
        {
            return weChatKeywordData.Delete(WeChatKeywordId);
        }
        /// <summary>
        /// 获取一条微信关键字信息
        /// </summary>
        /// <param name="WeChatKeywordId">指定的WeChatKeyword信息Id</param>
        /// <returns></returns>
        public WeChatKeywordDto GetWeChatKeywordById(int WeChatKeywordId)
        {
            WeChatKeywordEntity entity = this.weChatKeywordData.GetWeChatKeywordById(WeChatKeywordId);
            string websiteHost = configuration["CurrentlyImageHost"];
            entity.LocalUrl = string.Concat(websiteHost, entity.LocalUrl);
            return entity.As<WeChatKeywordDto>();
        }
        /// <summary>
        /// 分页获取微信关键字信息
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="index">分页页码</param>
        /// <param name="size">分页页数</param>
        /// <param name="ascending">排序</param>
        /// <returns></returns>
        public PageableList<WeChatKeywordDto> GetWeChatKeywordList(string keyword, DateTime? start,DateTime? end,int index, int size,  bool? ascending)
        {
            var list = weChatKeywordData.GetWeChatKeywordList(keyword, start, end, out int dataCount, ascending, index, size);
            string websiteHost = configuration["CurrentlyImageHost"];
            foreach (var item in list)
            {
                item.LocalUrl = string.Concat(websiteHost, item.LocalUrl);
            }
            return new PageableList<WeChatKeywordDto>
            {
                Count = dataCount,
                Items = list.As<IEnumerable<WeChatKeywordDto>>()
            };
        }
        /// <summary>
        /// 更新一条微信关键字信息
        /// </summary>
        /// <param name="WeChatKeywordId">指定的WeChatKeyword信息Id</param>
        /// <param name="dto">WeChatKeyword信息</param>
        /// <returns></returns>
        public WeChatKeywordDto Update(int WeChatKeywordId, WeChatKeywordRequestDto dto)
        {
            if (!this.weChatKeywordData.CheckKeywordExists(WeChatKeywordId))
            {
                throw new ArgumentException("此关键字不存在。");
            }
            WeChatKeywordEntity entity = dto.As<WeChatKeywordEntity>();
            entity.WeChatKeywordId = WeChatKeywordId;
            entity =weChatKeywordData.Update(entity);
            string websiteHost = configuration["CurrentlyImageHost"];
            entity.LocalUrl = string.Concat(websiteHost, entity.LocalUrl);
            return entity.As<WeChatKeywordDto>();
        }
        /// <summary>
        /// 获取所有微信关键字信息
        /// </summary>
        /// <returns></returns>
        public IEnumerable<WeChatKeywordDto> GetWeChatKeywordAll()
        {
            var list = weChatKeywordData.GetWeChatKeywordAll();
            string websiteHost = configuration["CurrentlyImageHost"];
            foreach (var item in list)
            {
                item.LocalUrl = string.Concat(websiteHost, item.LocalUrl);
            }
            return list.As<IEnumerable<WeChatKeywordDto>>();
        }
    }
}
